﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IRepository;
using MyBlog.IService;
using MyBlog.Model;
using MyBlog.Model.DTO;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebServe_demo.Utility;

namespace WebServe_demo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class BlogNewsController : ControllerBase
    {
        private readonly IBlogNewsRepository _iBlogNewsRepository;

        public BlogNewsController(IBlogNewsRepository iBlogNewsRepository)
        {
            this._iBlogNewsRepository = iBlogNewsRepository;
        }
        [HttpGet("BlogNews")]
        public async Task<ActionResult<ApiResult>> GetBlogNews()
        {
            int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            var data = await _iBlogNewsRepository.QueryAsync(c=>c.WriterId==id);
            if (data == null) return ApiResultHelper.Error("没有更多文章");
            return ApiResultHelper.Success(data);
        }
        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title,string content,int typeid)
        {
            //数据验证
            BlogNews blogNews = new BlogNews
            {

                BrowseCount = 0,
                Content = content,
                LikeCount = 0,
                Time = DateTime.Now,
                Title = title,
                TypeId = typeid,
                WriterId = Convert.ToInt32(this.User.FindFirst("Id").Value)

            };
            bool b = await _iBlogNewsRepository.CreateAsync(blogNews);
            if(!b) return ApiResultHelper.Error("添加失败，服务器发生错误！");
            return ApiResultHelper.Success(blogNews);
        }
        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id )
        {
            //数据验证

            bool b = await _iBlogNewsRepository.DeleteAsync(id);
            if (!b) return ApiResultHelper.Error("删除失败");
            return ApiResultHelper.Success(b);
        }
        [HttpPut("Edit")]
        public async Task<ActionResult<ApiResult>> Edit (int id ,string title,string content,int typeid)
        {
            var blogNews = await _iBlogNewsRepository.FindAsync(id);
            if (blogNews == null) return ApiResultHelper.Error("没有找到该文章");
            blogNews.Title = title;
            blogNews.Content = content;
            blogNews.TypeId = typeid;
            bool b = await _iBlogNewsRepository.EditAsync(blogNews);
            if (!b) return ApiResultHelper.Error("修改失败");
            return ApiResultHelper.Success(blogNews);

        }
        [HttpGet("BlogNewsPage")]
        public async Task<ApiResult> GetBlogNewsPage([FromServices]IMapper iMapper, int page,int size)
        {
            
            RefAsync<int> total = 0;
          var blognews =await _iBlogNewsRepository.QueryAsync(page, size, total);
            try
            {
                var blognewsDTO = iMapper.Map<List<BlogNewsDTO>>(blognews);
                return ApiResultHelper.Success(blognewsDTO, total);

            }
            catch (Exception)
            {
                return ApiResultHelper.Error("AutoMapper映射错误");
            }


        }


    }
}
